<template>
  <div class="contract-btn-list">
    <div class="btn-list-contetn">
      <div v-if="type == 'menuItem'">
        <div v-for="(item, index) in btnList" :key="index">
          <a-menu-item
            class="ant-dropdown-menu-item"
            @click="item.clickName"
            v-if="conditioncallBack(item)"
          >
            <content-text :text-info="item"></content-text>
          </a-menu-item>
        </div>
      </div>
      <div v-if="type == 'btn'">
        <p v-for="(item, index) in btnList" :key="index" class="inline">
          <a-button
            class="detail-btn "
            @click="item.clickName"
            v-if="conditioncallBack(item)"
          >
            <content-text :text-info="item"></content-text>
          </a-button>
        </p>
      </div>
    </div>
  </div>
</template>
<script>
import {
  downloadContractMethod,
  contractUploadDoubleMethod,
  contractRelocateMethod,
  contractSigningMethod,
  contractDetailMethod,
  contractBackToMethod
} from "../common/index";
import { verificationSealPopover } from "@/util/common";

const ContentText = {
  //合同签署按钮
  template: `
   <div class="inline">
              <p class="inline" v-if="textInfo.slotText">
                <a-tooltip placement="top">
                  <template slot="title">
                    <span>{{ textInfo.signTips }}</span>
                  </template>
                  {{ textInfo.title }}
                  (<span class="danger fs-10"> {{ textInfo.slotText }}</span
                  >)
                </a-tooltip>
              </p>
              <span v-else>{{ textInfo.title }}</span>
            </div> 
    `,
  name: "ContentText",
  props: {
    textInfo: {
      type: Object,
      default: () => ({})
    },
    signTips: {
      type: String,
      default: ""
    }
  }
};

export default {
  components: {
    ContentText: ContentText
  },
  props: {
    btnData: {
      type: Object,
      default: () => {
        return {};
      }
    },
    identity: {
      type: Number,
      default: 1
    },
    type: {
      type: String,
      default: "menuItem"
    }
  },
  data() {
    return {};
  },

  computed: {
    userInfo() {
      return this.$store.state.user;
    },
    btnList() {
      let res = [
        {
          title: "合同签署",
          clickName: this.contractSigning,
          condition:
            (this.btnData.contractType == 1 ||
              (this.btnData.contractType == 2 && this.identity == 1) ||
              (this.btnData.contractType == 3 && this.identity == 2)) &&
            this.btnData.isAdmin == true &&
            this.btnData.showSignContract == true,
          buyerAuthority: "buyerContractSign_btn",
          sellerAuthority: "sellerContractSign_btn",
          slotText: "限时免费",
          signTips:
            "需上传合同章和法人章，签署的时候合同章和法人章一并盖到合同上。"
        },
        // {
        //   title: "手绘签署",
        //   clickName: this.handPaintedSign,
        //   condition:
        //     (this.btnData.contractType == 1 ||
        //       (this.btnData.contractType == 2 && this.identity == 1) ||
        //       (this.btnData.contractType == 3 && this.identity == 2)) &&
        //     this.btnData.isAdmin == true &&
        //     this.btnData.showSignContract == true,
        //   buyerAuthority: "buyerContractSign_btn",
        //   sellerAuthority: "sellerContractSign_btn",
        //   slotText: "限时免费",
        //   signTips:
        //     "需上传合同章，进行手绘签名，签署的时候合同章和手绘签名结果一并盖到合同上。"
        // },
        {
          title: "上传双签合同",
          clickName: this.contractUploadDouble,
          condition:
            this.btnData.contractType == 1 ||
            this.btnData.contractType == 2 ||
            this.btnData.contractType == 3,
          buyerAuthority: "buyerContractUploadDouble_btn",
          sellerAuthority: "sellerContractUploadDouble_btn"
        },
        {
          title: "合同打回",
          clickName: this.contractBackTo,
          condition:
            this.btnData.contractType == 2 || this.btnData.contractType == 3,
          buyerAuthority: "buyerContractBackTo_btn",
          sellerAuthority: "sellerContractBackTo_btn"
        },
        {
          title: "合同撤销",
          clickName: this.contractRelocate,
          condition: this.btnData.contractType == 1,
          buyerAuthority: "buyerContractRelocate_btn",
          sellerAuthority: "sellerContractRelocate_btn"
        },
        {
          title: "下载合同",
          clickName: this.contractDownload,
          condition: this.btnData.contractType !== 5,
          buyerAuthority: "buyerContractDownload_btn",
          sellerAuthority: "sellerContractDownload_btn"
        }
      ];
      return res;
    }
  },
  created() {
    // console.log("this.btnData", this.btnData);
  },
  methods: {
    downloadContractMethod,
    contractUploadDoubleMethod,
    contractRelocateMethod,
    contractSigningMethod,
    contractDetailMethod,
    contractBackToMethod,
    verificationSealPopover,
    conditioncallBack(item) {
      let res =
        item.condition &&
        (this.$verifyBtn(item.buyerAuthority) ||
          this.$verifyBtn(item.sellerAuthority));
      return res;
    },
    //下载合同
    contractDownload() {
      this.downloadContractMethod(this.btnData.id);
    },
    //上传双签合同
    contractUploadDouble() {
      this.contractUploadDoubleMethod(this.btnData, this.identity);
    },
    //合同打回
    contractBackTo() {
      this.contractBackToMethod(
        this.btnData.id,
        this.btnData.version,
        this.identity
      ).then(res => {
        if (res) {
          this.$emit("initPage");
        }
      });
    },
    //合同撤销
    contractRelocate() {
      this.contractRelocateMethod(
        this.btnData.id,
        this.btnData.version,
        this.identity
      ).then(res => {
        if (res) {
          this.$emit("initPage");
        }
      });
    },
    //合同签署
    contractSigning() {
      this.contractSigningMethod(
        this.identity,
        this.btnData.id,
        this.btnData.version
      )
        .then(res => {
          if (res.shortUrl) {
            this.$emit("getWillModalData", "will", res);
          }
        })
        .catch(err => {
          this.verificationSealPopover(err);
        });
    },
    //手绘签署
    handPaintedSign() {
      this.$emit("openHandPaintedModal", "hand");
    }
  }
};
</script>

<style lang="scss" scoped>
.detail-btn {
  margin-right: 8px;
}
</style>
